import os

from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class Notes(db.Model):
  title = db.StringProperty(multiline = True)
  desc = db.TextProperty()
  created = db.DateTimeProperty(auto_now_add=True)

class MainHandler(webapp.RequestHandler):

  def get(self):
    notes = Notes.all().order('-created')
    
    view_path = os.path.join(os.path.dirname(__file__), 'view/notes.htm')
    
    view_values = {
      'notes' : notes,
      }
    
    self.response.out.write(template.render(view_path, view_values))

class LayoutTest(webapp.RequestHandler):
  def get(self):
    notes = Notes.all().order('-created')
    view_path = os.path.join(os.path.dirname(__file__), 'view/notes_view.htm')
    
    view_values = {
      }

    self.response.out.write(template.render(view_path, view_values))

class PostValue(webapp.RequestHandler):

  def post(self):
    notes = Notes()
    
    notes.title = self.request.get('title')
    notes.desc = self.request.get('desc')
    notes.put()
    self.redirect('/notes/')

def main():
  application = webapp.WSGIApplication([('/notes', MainHandler),
                                    ('/notes/', MainHandler),
                                    ('/notes/post', PostValue),
                                    ('/notes/layout', LayoutTest)
                                    ],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
